package com.hspedu.newData.dmsxl.树.二叉树路径.路径;

import com.hspedu.newData.dmsxl.ds.TreeNode;
import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-29 
 * @Version: 1.0
 */

public class LC129求根节点到叶节点数字之和 {


    // todo
    public int sumNumbers(TreeNode root) {
        int sum = sumN(root, 0);
        return sum;
    }


    public int sumN(TreeNode root,int pre){
        if (root == null){
            return 0;
        }
        int sum = pre*10 + root.val;
        if (root.left == null && root.right == null){
            return sum;
        }else {
            return sumN(root.left,sum) + sumN(root.right,sum);
        }
    }


    @Test
    public void test() {
        // 测试代码

        TreeNode t5 = new TreeNode(5);
        TreeNode t4 = new TreeNode(4);
        TreeNode t3 = new TreeNode(3,null,null);
        TreeNode t2 = new TreeNode(2,t4,t5);
        TreeNode t1 = new TreeNode(1,t2,t3);

        int i = sumNumbers(t1);
        System.out.println(i);
    }

}
